home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 907 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: initialization of nonlocal static objects
  5. Date: 29 Mar 1996 09:06:16 PST
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Mar29120031@gabi.gabi-soft.fr>
  9. References: <009A0041E919D6E0.3D60EEDA@ittpub.nl>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 29 Mar 1996 11:00:31 GMT
  12. In-reply-to: "Wil Evers"'s message of 28 Mar 96 16:20:31 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMVwYiky4NqrwXLNJAQE79QH/bH0Mdxg1l8kohUbRwbUkxMZXhI+o3w9g
  15.     TUEtsfIWXY8veTKP1WXlut+W1zsbGEEwPwgv/Z4NNSwXgeoMRTqi7A==
  16.     =XJ7x
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article <009A0041E919D6E0.3D60EEDA@ittpub.nl> "Wil Evers"
  20. <wil@ittpub.nl> writes:
  21.  
  22. |> In article <KANZE.96Mar27134634@gabi.gabi-soft.fr> kanze@gabi-soft.fr (J.
  23. |> Kanze) writes:
  24.  
  25. |> >     class X
  26. |> >     {
  27. |> >     public :
  28. |> >         enum DontInit { dontInit } ;
  29. |> >                         X( DontInit )   {}
  30. |> >                         X() : i( 5 )    {}
  31. |> >     private :
  32. |> >         int             i ;
  33. |> >     } ;
  34. |> > 
  35. |> > Objects to be initialized by the nifty_counter would be defined thus:
  36. |> > 
  37. |> >     X               obj( X::dontInit ) ;
  38. |> > 
  39. |> > Nifty counter then becomes:
  40. |> > 
  41. |> >     class NiftyCounter
  42. |> >     {
  43. |> >         static int          cnt ;
  44. |> >     public :
  45. |> >                             NiftyCounter()
  46. |> >         {
  47. |> >             if ( cnt == 0 )
  48. |> >                 new( &obj ) X ;
  49. |> >             cnt ++ ;
  50. |> >         }
  51. |> >     } ;
  52. |> > 
  53. |> > (Note that for NiftyCounter to work, it is absolutely essential that the
  54. |> > definition of the object invoke a no-op constructor, since the object
  55. |> > may in fact already be constructed.)
  56. |> > 
  57. |> > As you can see, this works equally well for const objects.
  58.  
  59. |> I don't think so. For this to work on a const object, you would at least have
  60. |> to cast away its constness - after all, the standard placement new operator
  61. |> takes a void * as its second argument, not a const void *.
  62.  
  63. You're right.  I'm getting careless.  (And the last time I did this, my
  64. compiler still didn't enforce const-ness in this case.)
  65.  
  66. |> In my opinion, the `dummy constructor' technique is fundamentally bad. It
  67. |> requires that all base classes and member variables have dummy constructors
  68. |> too, so it is not always applicable.
  69.  
  70. |> When I use the nifty counter idiom, I normally construct the global
  71. |> object in a suitably aligned raw memory buffer (a character array).
  72. |> Users access the constructed object through a properly typed global
  73. |> reference pointing to the raw memory buffer.
  74.  
  75. IMHO, the nifty counter idiom is in general not very good.  I much
  76. prefer to avoid the global or class static, and require the user to call
  77. a function which returns a reference to a local static.
  78. -- 
  79. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  80. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  81. Conseils en informatique industrielle --
  82.                             -- Beratung in industrieller Datenverarbeitung
  83. ---
  84. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  85.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  86.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  87.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  88.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  89. ]
  90.